﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Sellers;
using ZBJF.Warranty.WebProvider;
using ZBJF.Warranty.WebProvider.Filter;

namespace ZBJF.Warranty.H5.Controllers
{
    [LoginFilter(IsCheck = false)]
    public class LoginController : BaseController
    {
        // GET: Login
        public ActionResult Index()
        {
            if (CurrentUser != null)
                return RedirectToAction("Index", "Home");
            return View();
        }

        [HttpPost]
        [LoginFilter(IsCheck = false)]
        [ValidateAntiForgeryToken]
        public ActionResult Index(SystemUser model)
        {
            bool flag;
            var user = ServiceFactory.SellerEmployeeServiceInstance.Login(model.UserAccount, model.Password, out flag);
            if (flag)
            {
                CurrentLoginUser.CurrentUser = user;
                CookiesHelper.Instance.SetLoginSession(user.UserAccount);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                CurrentUser = null;
                CookiesHelper.Instance.ClearLoginSession();
                TempData["success"] = false;
                TempData["Msg"] = "用户名或者密码无效！";
                return View("Index");
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult LogOut()
        {
            CurrentUser = null;
            CookiesHelper.Instance.ClearLoginSession();
            return RedirectToAction("Index", "Login");
        }
        public ActionResult ForgetPassword()
        {
            return View();
        }
        [HttpPost]
        public ActionResult NextToModifyPassword(SellerEmployeeModel model)
        {
            var success = ServiceFactory.SendSmsVerificationCodeLogServiceInstance.ValidatePhoneCode(model.UserPhone,
               model.VerificationCode, 5);
            if (!success)
            {
                TempData["success"] = false;
                TempData["Msg"] = "您输入的验证码有误或已失效（请在收到验证码后5分钟内使用）！";
                return RedirectToAction("ForgetPassword", "Login");
            }
            return RedirectToAction("ModifyPassword", "SellerEmpoyee", new { ModifyPasswordType = 1, UserPhone = model.UserPhone, VerificationCode = model.VerificationCode });

        }
    }
}